package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LevelServiceImpl extends ServiceImpl<LevelMapper, LevelDO> implements LevelService {
    @Autowired
    LevelConverter levelConverter;

    @Autowired
    LevelMapper levelMapper;
    @Override
    public void addLevel(LevelCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        save(levelDO);
    }

    @Override
    public void updateLevel(LevelCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        saveOrUpdate(levelDO);
    }

    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        if (request == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        Integer pageNum = request.getPageNum();
        Integer count = request.getCount();
        String keyword = request.getKeyword();

        Page<LevelDO> levelDOPage = new Page<>(pageNum, count);
        LambdaQueryWrapper<LevelDO> wrapper = new LambdaQueryWrapper<LevelDO>()
                .like(keyword !=null, LevelDO::getLevelName, keyword);
        Page<LevelDO> page = levelMapper.selectPage(levelDOPage, wrapper);

        List<LevelDO> levelDOList = page.getRecords();

        long total = page.getTotal();
        return levelConverter.levelDOToPageDTO(levelDOList, (int) total);
    }

    @Override
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        removeById(id);
        LevelDO levelDO = getById(id);
        return levelConverter.levelDOToDeleteLevelOperateDTO(levelDO);

    }

    @Override
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        removeByIds(ids);
        List<LevelDO> levelDOList = listByIds(ids);
        return levelConverter.levelDOsToDeleteLevelOperateDTOs(levelDOList);

    }
}
